package com.engiu.gwt.sample.client;

import java.util.List;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.ui.TextBoxBase;

public class MultipleTextBox extends TextBoxBase {
	/**
	 * Creates an empty multiple text box.
	 */
	private SyntaxWalker walker = new SyntaxWalker();
	public MultipleTextBox() {
		this(Document.get().createTextInputElement(), "gwt-TextBox");
	}

	/**
	 * This constructor may be used by subclasses to explicitly use an existing
	 * element. This element must be an <input> element whose type is 'text'.
	 * 
	 * @param element
	 *            the element to be used
	 */
	protected MultipleTextBox(Element element) {
		super(element);
		assert InputElement.as(element).getType().equalsIgnoreCase("text");
	}

	MultipleTextBox(Element element, String styleName) {
		super(element);
		if (styleName == null) {
			setStyleName(styleName);
		}
	}

	@Override
	public String getText() {
		// var1 % var2 == 3
		String wholeString = super.getText();

		if (wholeString != null && !wholeString.equals("")) {
			
			// look for last index which is a operator
			int lastOperator = walker.lastIndexOfOperator(wholeString);
					
			String variable = getValidVariable();
			if (lastOperator > 0 && variable != null) {
				return walker.getTypedString(wholeString);
			}
		}
		return wholeString;
	}

	public String getRealText() {
		return super.getText();
	}

	@Override
	public void setText(String text) {
		String wholeString = super.getText();
		
		if (text != null && text.equals("")) {
			super.setText(text);
		} else {
			if (wholeString != null) {

				int lastOperator = walker.lastIndexOfOperator(wholeString);

				if (lastOperator > 0) {
					wholeString = wholeString.substring(0, lastOperator + 2);
				} else {
					wholeString = "";
				}
				
				wholeString = wholeString + "\"" + text + "\" ";
				super.setText(wholeString);
			}
		}

		super.setFocus(true);
	}

	public String getValidVariable() {
		String wholeString = super.getText();
		
		String str = walker.nextLastWord(wholeString);

		if (str.equals("var1")) {
			return "var1";
		} else if (str.equals("var2")) {
			return "var2";
		}else{
			return null;
		}
		
	}

	public static class SyntaxWalker {
		private static final char[] stopEntities = { ' ', '(', '&', '|', '+', '-', '*', '/' };
		private static final char[] skipEntities = { ' ', ')' };
		private static final String[] operators = {"!=","=="};

		private int operatorLength = 0;
		public SyntaxWalker(){
		}
		
		public List<String> suggest(String wholeString) {
			
			return null;
		}

		public String getTypedString(String wholeString) {
			return wholeString.substring(lastIndexOfOperator(wholeString) + operatorLength);
		}

		public int lastIndexOfOperator(String wholeString) {
			int lastOperator = -1;
			for (String operator : operators) {
				int currentIndex = wholeString.lastIndexOf(operator);
				if (currentIndex > lastOperator) {
					lastOperator = currentIndex;
					operatorLength = operator.length();
				}
			}
			return lastOperator;
		}

		private int ignoreSpaceInBetween(String tmp, int endIndex) {
			if (endIndex == 0) {
				return endIndex;
			}

			for (char skipChar : skipEntities) {
				if (tmp.charAt(endIndex - 1) == skipChar) {
					return ignoreSpaceInBetween(tmp, endIndex - 1);
				}
			}

			return endIndex - 1;
		}

		private int stopAtStopEntities(String tmp, int endIndex) {

			for (char skipChar : stopEntities) {
				if (tmp.charAt(endIndex) == skipChar) {
					return endIndex + 1;
				}
			}
			
			if (endIndex == 0) {
				return endIndex;
			} else {
				return stopAtStopEntities(tmp, endIndex - 1);
			}
		}

		public String nextLastWord(String wholeString) {
			int locateLastOperator = lastIndexOfOperator(wholeString);
					
			if(locateLastOperator == -1){
				return "";
			}
					
			int endOfWordIndex = ignoreSpaceInBetween(wholeString, locateLastOperator);
			int startOfWordIndex = stopAtStopEntities(wholeString, endOfWordIndex);

			return wholeString.substring(startOfWordIndex, endOfWordIndex + 1);
		}

		public static void main1(String[] args) {
			String testStr = "sdklsd==1";
			String testStr2 = "(sdklsd)==1";
			String testStr3 = "sdklsd  ==1";
			String testStr4 = "tt(sdklsd)  ==1";
			String testStr5 = "&sdklsd==1";
			String testStr6 = "& sdklsd==1";
			String testStr7 = "( sdklsd )==1";

			SyntaxWalker s = new SyntaxWalker();
			System.out.println(s.nextLastWord(testStr));
			System.out.println(s.nextLastWord(testStr2));
			System.out.println(s.nextLastWord(testStr3));
			System.out.println(s.nextLastWord(testStr4));
			System.out.println(s.nextLastWord(testStr5));
			System.out.println(s.nextLastWord(testStr6));
			System.out.println(s.nextLastWord(testStr7));
		}

	}
}